package com.ymttest.database.sqlwapper;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtProductsInActivity;

/************************************************************************************
 * @File name : YmtUserWapper.java
 * @Author : zhouyi
 * @Date : 2015年4月2日
 * @Copyright : 洋码头
 ************************************************************************************/
/************************************************************************************
 * @File name : YmtProductsInActivityWapper.java
 * @Author : zhouyi
 * @Date : 2015年5月22日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtProductsInActivityWapper {
	SqlManager manage;

	public YmtProductsInActivityWapper() {
		manage = new SqlManager("intpro");
	}

	// ipromotiontype 1 折扣 2 直降 3 折后价
	/**
	 * 新增一条ProductsInActivity记录
	 * 
	 * @param iactivityid
	 *            活动id
	 * @param sproductid
	 *            productid,
	 * @param ipromotiontype
	 *            1 折扣 2 直降 3 折后价
	 * @param iuserid
	 *            活动发布sellerid
	 * @return
	 * @throws InterruptedException
	 */
	public int insertOneProductsInActivity(int iactivityid, String sproductid, int ipromotiontype, int iuserid)
			throws InterruptedException {
		Calendar calendar = Calendar.getInstance();
		YmtProductsInActivity record = new YmtProductsInActivity();
		record.setIactivityid(iactivityid);
		record.setSproductid(sproductid);
		record.setDaddtime(calendar.getTime());
		record.setDbegintime(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		record.setDendtime(calendar.getTime());
		record.setIactivitystock(5);
		record.setIpromotiontype(ipromotiontype);
		record.setFpromotion("0.9");
		record.setIstatus(2);
		record.setSmarketprice(new BigDecimal(10.00).toString());
		record.setSactivityproductname("自动化测试活动商品名Sactivityproductname");
		record.setSdescription("自动化测试商品卖点说明sDescription");
		record.setIuserid(iuserid);
		record.setFpromotion1("0.8");
		record.setFpromotion2("0.7");
		record.setFpromotion3("0.6");
		record.setBuseuserlevelprice(true);
		record.setBissecondsell(true);
		record.setIuserid(3);
		record.setBuseuserlevelprice(true);
		record.setSmemo("自动化测试用活动");
		int i = manage.insert("com.ymttest.database.mapping.YmtProductsInActivityMapper.insert", record);
		manage.commit();
		// manage.close();
		return i;
	}

	/**
	 * 用iActivityId查询YmtProductsInActivity记录
	 * 
	 * @param aid
	 *            iActivityId
	 * @return 一条YmtProductsInActivity记录
	 * @throws InterruptedException
	 */
	public YmtProductsInActivity selectByiActivityId(int aid) throws InterruptedException {
		List<YmtProductsInActivity> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectByiActivityId", aid);
		// manage.close();
		return pia.get(0);
	}
	
	public YmtProductsInActivity selectByProductInActivityId(int productInActivityId) throws InterruptedException {
		List<YmtProductsInActivity> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectByProductInActivityId", productInActivityId);
		// manage.close();
		return pia.get(0);
	}
	

	
	public List<YmtProductsInActivity> selectAllByiActivityId(int aid) throws InterruptedException {
		List<YmtProductsInActivity> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectByiActivityId", aid);
		// manage.close();
		return pia;
	}
	
	/**
	 * 此方法实际会返回iSubsidyType=1的活动商品列表 返回信息可以用于验证返回结果，计算价格等
	 * 
	 * @return HashMap Just like {loginId=wesper,
	 *         CatalogId=ed939223-4255-4bda-a985-52098e795160, bOnly4VIP=false,
	 *         fSubsidyPrice=9.98, iActivityTemplateId=10, deliveryTemplateId=,
	 *         bGiftAvail4Reward=true, iProductInActivityId=9786, SellerId=3383,
	 *         fPromotion3=0.60, ProductName=念书, fPromotion2=0.70,
	 *         validStart=2013-07-04 10:15:02.56, fPromotion1=0.80, iNum=100,
	 *         Picture=http://p5.img.ymatou.com/upload/shangou/original/2d
	 *         a4aa4d4237420a90ff150d3631fd2a_o.jpg, fPromotion=0.90,
	 *         iActivityId=3802, fFlight=1.00, iSubsidyType=1,
	 *         ProductId=e55f5cf4-b568-4cea-99bb-13846cad9131, iCatalogStatus=2,
	 *         validEnd=2015-12-01 00:00:00.0, iPromotionType=1,
	 *         bGiftAvail4OrderDeduct=true, iAction=0, iProductsSubsidyId=464,
	 *         iLimitNum=100, fWeight=0.00, sName=自动化测试新人活动特惠, iCatalogType=0,
	 *         fQuotePrice=60.00}
	 * @throws InterruptedException
	 */
	public List<Map> selectNewActiveProductInfo() throws InterruptedException {
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectProductInActivityInfo", "");
		// manage.close();
		return pia;
	}

	/**
	 * @param cid
	 *            产品CatalogId
	 * @return See YmtProductsInActivityWapper#selectNewActiveProductInfo
	 * @see YmtProductsInActivityWapper#selectNewActiveProductInfo
	 * @throws InterruptedException
	 */
	public List<Map> selectProductInActivityInfoByCatalogId(String cid) throws InterruptedException {
		// 清空缓存
		manage.session.clearCache();
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectProductInActivityInfoByCatalogId", cid);
		// manage.close();
		return pia;
	}

	/**
	 * @param cid
	 *            产品CatalogId
	 * @return See YmtProductsInActivityWapper#selectNewActiveProductInfo
	 * @see YmtProductsInActivityWapper#selectNewActiveProductInfo
	 * @throws InterruptedException
	 */
	public List<Map> selectProductInActivityInfoBySellerId(int sid) throws InterruptedException {
		List<Map> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectProductInActivityInfoBySellerId", sid);
		// manage.close();
		return pia;
	}

	/**
	 * 查询一个ProductList 返回sProductid list中的id有参加活动
	 * 
	 * @return 活动ProductList
	 * @throws InterruptedException
	 */
	public List<String> selectProductListInActivity() throws InterruptedException {
		List<String> pia = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsInActivityMapper.selectProductListInActivity", null);
		// manage.close();
		return pia;
	}

	
	
	public YmtProductsInActivity selectByProductId(String productId) throws InterruptedException{
		
		List<YmtProductsInActivity> pia = manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInActivityMapper.selectByProductId", productId);
		manage.commit();
		int redo=0;
		while(pia.size()==0&&redo<5){
			redo++;
			Thread.sleep(5000);
			 pia = manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInActivityMapper.selectByProductId", productId);
			 manage.commit();
		}
		return pia.get(0);
	}
	
	
	public YmtProductsInActivity selectByProductIdAndActivityId(String productId,int activityId)throws InterruptedException{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("sProductId", productId);
		map.put("iActivityId", activityId);
		List<YmtProductsInActivity> pia = manage.getSelectResults("com.ymttest.database.mapping.YmtProductsInActivityMapper.selectByProductIdAndActivityId", map);
		return pia.get(0);
	}
	
	
	
	//更新istatus
	public int updateiStatus(int istatus, String sProductId) throws InterruptedException {
		HashMap m = new HashMap();
	    m.put("istatus", istatus);
	    m.put("sProductId", sProductId);
	    int i = manage.update("com.ymttest.database.mapping.YmtProductsInActivityMapper.updateiStatus", m);
	    manage.commit();
	    // manage.close();
	    return i;
	}
	
	
	//根据主键id 更新
	public int updateByPrimaryKey(YmtProductsInActivity record) throws InterruptedException {
	    int i = manage.update("com.ymttest.database.mapping.YmtProductsInActivityMapper.updateByPrimaryKey", record);
	    manage.commit();
	    // manage.close();
	    return i;
	}
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	/**
	 * 返回sqlmanager
	 * 
	 * @return
	 */
	public SqlManager getManager() {
		return manage;
	}

	
	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) throws Exception {
		System.out.println((new YmtProductsInActivityWapper().selectByProductInActivityId(11061).getSproductid().toString()));

	}
}
